Ext.namespace('VAS');

VAS.TemplateContentPanel = function(config) {
	this.config = config;
    VAS.TemplateContentPanel.superclass.constructor.call(this,config);
}



Ext.extend(VAS.TemplateContentPanel,Ext.FormPanel, {
	
	
	initComponent : function() {
		var config={
			bodyStyle:'padding:5px 15px 0 ',
            frame         : true,
		    minButtonWidth: 50,
		    buttonAlign   : 'center',
			labelAlign    : 'left',
            layout        : 'column',
            items         : [{
					layout: "table",
					columnWidth : 1,
					items: [
						{
							xtype           : "panel",
							layout          : "form",
							width           : 120,
							items           : {xtype: "label", fieldLabel: "文件名",	labelStyle:"text-align:left"}
						},{
							xtype           : 'textfield',
							hideLabel       : true,
							width           : 500,
	                   		id              : 'fileName',
	                   		name            : 'fileName',
	                   		disabled        : true
						}
					]
			}, {
					layout: "table",
					columnWidth : 1,
					items: [
						{
							xtype           : "panel",
							layout          : "form",
							width           : 120,
							items           : {xtype: "label", fieldLabel: "文件内容",	labelStyle:"text-align:left"}
						},{
							xtype           : 'textarea',
							hideLabel       : true,
		                    width           : 500,
		                    height          : 240,
	                   		id              : 'fileContent',
	                   		name            : 'fileContent'								
						}
					]
			},{
				xtype : 'hidden',
				name : 'fileId',				
				id : 'fileId'				
			}]
		};
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);
		VAS.TemplateContentPanel.superclass.initComponent.apply(this, arguments);
	},
	
	
	
	loadForm : function() {
		this.load({
			    waitTitle   : '正在加载中...',
				waitMsg     : '正在向服务器发送请求', 
	 			url         : this.config.loadUrl,
	 			success     : function(form,action) {	
		 			var fileName = action.result.data["fileName"];
		 			if(fileName == 'about.xml'){
		 				Ext.getCmp("fileContent").setReadOnly(true);
		 			}
	 			},
	 			failure     : function(form,action) {
	 				Ext.Msg.alert("失败",action.result.msg);
	 			}
	 	});				
	}
})


VAS.TemplateContentWindow = function($config) {
	var _this = this;
	
	_this._getHeight = function(_height,_y) {
		var $height;
		if (typeof(_height) !== "undefined") {
			if (typeof(_height)==='string'&&_height.indexOf('%') > -1) {
				$height = document.body.clientHeight -_y-180;			
			} else if(typeof(_height)==='number'){
				$height = _height - 50;
			}
		} 
		
		return $height;
	}
	
	_this._getWinHeight = function(_height,_y) {
		var $height;
		if (typeof(_height) === "string") {
			if (_height.indexOf('%') > -1) {
				var percent = _height.substring(0, _height.length - 1) / 100;
					$height = document.body.clientHeight -_y-156;	
			} else {
				$height = _height ;
			}
		} 
		return $height;

	};
	
	_this._getWinWidth = function(_width,_x) {			
		var $width;
		if (typeof(_width) !== "undefined") {
			if (typeof(_width)==='string'&&_width.indexOf('%') > -1) {
			$width = document.body.clientWidth -_x-130;	
			} else if(typeof(_width)==='number'){
				$width = _width;
			}
		} 
		return $width;
	};
	
	var form  = new VAS.TemplateContentPanel({
		url          : $config.formPanel.storeUrl,
		loadUrl      : $config.formPanel.loadUrl,
		height       : _this._getHeight($config.height,$config.y) - 34
	});
	
	
	form.loadForm();
	
	var thisConfig = {
		closable      : false,
		autoScroll    : true,
		layout        : 'fit',
		items         : form,
		height        : _this._getWinHeight($config.height,$config.y),
		width         : _this._getWinWidth($config.width,$config.x),
    	buttons : [{
	       id      : 'saveBt' ,
	       text    : '保存',
	       handler : function() {
	       	
				if(form.getForm().isValid()){
						form.getForm().submit({
				              url              : $config.formPanel.storeUrl,
							  waitTitle        : '正在提交',
				              waitMsg          : '正在向服务器发送请求',
				              clientValidation : true,
							  success :function(form,action){
									Ext.Msg.alert('提示','提交保存成功!',function(){ 
										
									});
							  },
							  failure :function(form,action){
									if(action.failureType == Ext.form.Action.CLIENT_INVALID){
										Ext.Msg.alert('提示','输入的信息不符合规格，请按照提示重新输入！');
									}else{
										alertFailure(action.result);
									}
							 }		
				        })
				  }		       
	       }
	    },{
	       text    : '关闭',
	       handler : function(){
				_this.close();
	       }
	    }]
	};
	
	var _config = $config || {};
	Ext.apply(_this, Ext.apply(_config, thisConfig));
	VAS.TemplateContentWindow.superclass.constructor.call(_this);
	
	

};
Ext.extend(VAS.TemplateContentWindow,VAS.BasicWindow,{
});


Ext.reg('templatecontent-win',VAS.TemplateContentWindow);

